package com.heima.leetcode.practice;

/**
 * @author 勾新杰
 * @version 1.0
 * @description: leetcode 24. 两两交换链表中的节点
 * @date 2025/4/23 10:48
 */
public class E24 {

    /**
     * 递归
     *
     * @param head 链表头结点
     * @return 链表
     */
    public ListNode swapPairs(ListNode head) {
        ListNode sentinel = new ListNode(-1);
        sentinel.next = head;
        ListNode curr = head;
        ListNode prev = sentinel;
        while (curr != null && curr.next != null) {
            ListNode next = curr.next;
            curr.next = next.next;
            next.next = curr;
            prev.next = next;
            prev = curr;
            curr = curr.next;
        }
        return sentinel.next;
    }
}
